﻿using System;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class MSSQL : IDb
    {
        SqlConnection conn = new SqlConnection();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ServerName"></param>
        /// <param name="dbName"></param>
        /// <param name="userID"></param>
        /// <param name="pwd"></param>
        public MSSQL(string ServerName, string dbName, string userID, string pwd)
        {
            conn.ConnectionString = "Data Source=" + ServerName + ";" +
                                    "Initial Catalog=" + dbName + ";" +
                                    "User id=" + userID + ";" +
                                    "Password=" + pwd + ";";
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public System.Data.DataTable ExecuteSelectCommand(IDbCommand command)
        {
            DataTable result = new DataTable();

            try
            {
                SqlCommand sqlCommand = (SqlCommand)command;
                sqlCommand.Connection = conn;
                sqlCommand.Connection.Open();
                sqlCommand.CommandType = CommandType.Text;
                SqlDataReader sqlReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                result.Load(sqlReader);
                sqlReader.Close();
                return result;
            }
            catch
            {
                throw;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selectStatement"></param>
        /// <returns></returns>
        public System.Data.DataTable ExecuteSelectCommand(string selectStatement)
        {
            DataTable result = new DataTable();
            SqlCommand sqlCommand = null;
            try
            {
                conn.Open();
                sqlCommand = new SqlCommand(selectStatement);
                sqlCommand.CommandType = CommandType.Text;
                SqlDataReader sqlReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                result.Load(sqlReader);
                sqlReader.Close();
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selectStatement"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataTable ExecuteSelectCommand(string selectStatement, string[,] parameters)
        {
            DataTable result = new DataTable();
            try
            {
                SqlCommand sqlCommand = new SqlCommand(selectStatement);

                for (int i = 0; i < parameters.Length; i++)
                {
                    sqlCommand.Parameters.AddWithValue(parameters[i, 0], parameters[0, i]); //parameters[name, value]
                }

                conn.Open();
                SqlDataReader sqlReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                result.Load(sqlReader);
                sqlReader.Close();
                return result;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public string ExecuteScalar(System.Data.Common.DbCommand command)
        {
            throw new NotImplementedException();
        }

        public string ExecuteScalar(string selectStatement)
        {
            throw new NotImplementedException();
        }
    }
}
